package org.jeecg.modules.maintain.controller;

import org.jeecg.modules.demo.mes.entity.ManageDevice;
import org.jeecg.modules.demo.mes.service.IManageDeviceService;
import org.jeecg.modules.maintain.entity.*;
import org.jeecg.modules.maintain.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {

    @Autowired
    private IManageDeviceService manageDeviceService;


    @Autowired
    private ICamelliaDevicePatrolService camelliaDevicePatrolService;


    @Autowired
    private ICamelliaDeviceRepairHistoryService camelliaDeviceRepairHistoryService;


    @Scheduled(cron = "0 0 1 * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {

        //查询设备今天是否维保
        List<ManageDevice>  list = manageDeviceService.selList();

        for (ManageDevice device: list) {

            //判断设备维保日期 是否是今天
            if(isNow(device.getNextTime())){

                System.out.println(device.getDeviceName());
                System.out.println("是今天");
                //添加设备维保信息
                    CamelliaDevicePatrol camelliaDevicePatrol = new CamelliaDevicePatrol();
                    camelliaDevicePatrol.setDeviceCode(device.getDeviceCode());
                    camelliaDevicePatrol.setPatrolTime(new Date());
                    camelliaDevicePatrol.setPatrolDesc("设备维保");
                    camelliaDevicePatrol.setUserName(device.getDeviceName());
                    camelliaDevicePatrol.setState("0");
                    //生成  1 + 18位唯一 数字
                    //camelliaDevicePatrol.setId("1");
                    camelliaDevicePatrolService.save(camelliaDevicePatrol);

                    //修改设备下次维保信息
                    Date date = dateAddOne(device.getNextTime(),device.getDaviceDay());
                    device.setNextTime(date);
                    device.setLastTime(new Date());
                    manageDeviceService.updateById(device);

                   //添加设备维保历史信息
                    CamelliaDeviceRepairHistory camelliaDeviceRepairHistory =new CamelliaDeviceRepairHistory();
                    camelliaDeviceRepairHistory.setCreateTime(new Date());
                    camelliaDeviceRepairHistory.setDeviceCycle(device.getDaviceDay().toString());
                    camelliaDeviceRepairHistory.setDeviceCode(device.getDeviceCode());
                    camelliaDeviceRepairHistory.setDeviceTime(new Date());
                    camelliaDeviceRepairHistoryService.save(camelliaDeviceRepairHistory);
            }

        }



        System.out.println("定时器");
    }
    /**
     * 实现日期加减
     * @param date
     * @return    返回加减后的日期
     */
    public static Date dateAddOne(Date date,int day) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,day); //把日期往后增加一天,整数  往后推,负数往前移动
        date=calendar.getTime(); //这个时间就是日期往后推一天的结果
        return date;

    }

    /**
     * 判断日期是不是今天
     * @param date
     * @return    是返回true，不是返回false
     */
    public static boolean isNow(Date date) {


        // 默认的年月日的格式. yyyy-MM-dd
        String PATTEN_DEFAULT_YMD = "yyyy-MM-dd";
        // 当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat(PATTEN_DEFAULT_YMD);
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
        String day = sf.format(date);

        return day.equals(nowDay);
    }

}
